﻿
// 获取当前日期时间
var now = new Date();
var year = now.getFullYear();
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var day = ("0" + now.getDate()).slice(-2);
var hours = ("0" + now.getHours()).slice(-2);
var minutes = ("0" + now.getMinutes()).slice(-2);
//var seconds = ("0" + now.getSeconds()).slice(-2);
var serialNumber = "PX" + year + month + day + hours + minutes;
var inputElement = document.getElementById('ProcessNo');
inputElement.value = serialNumber;

var obj1 = {
    processRouteId:"",
    "processPouteNumber": "",
    "processRouteName": "",
    "state": "",
    "productName": "",
    "createdBy": "",
    "createdDate": "",
    "updatedBy": "",
    "updatedDate": "",
    productionProcessesId: [],
    QualityprogramId:""
};

layui.use(['table', 'dropdown', 'layer', 'form'], function () {
    var table = layui.table;
    var dropdown = layui.dropdown;
    var layer = layui.layer;
    var form = layui.form;
    var laypage = layui.laypage;
    $("#DeviceTan").on('click', function () {
        DeviceTan()
    })
    //弹窗
    window.DeviceTan = function DeviceTan() {
       
        layer.open({
            type: 1, //page 层类型
            area: ['800px', '400px'],
            title: '新增工序',
            shade: 0.6, //遮罩透明度
            shadeClose: true, //点击遮罩区域，关闭弹层
            maxmin: true, //允许全屏最小化
            anim: 0, //0-6 的动画形式，- 1 不开启
            content: `
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">工序编号：</label>
            <div class="layui-input-inline">
                <input type="text" id="ProductionProcessesNumber" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div>
            <div class="layui-inline" class="layui-btn-container">

            <div class="layui-inline" style="margin-left:500px">
              
                <button type="button" class="layui-btn layui-bg-blue" onclick="ShowProcessProcedures()">查询</button>
                  <button type="button" class="layui-btn layui-bg-blue" onclick="DeviceAdd()">确认选中</button>
            </div>
           
             
                
               
        </div>
    </div>
   
<style>
    span{
        font-size:18px;
        margin-right:px;
    }
    </style>
    
<table class="layui-table" id="ProcessProceduresTable" width="100%"></table>`,
            success: function () {
                form.render()
                ShowProcessProcedures()
                //// 使用事件委托绑定点击事件
                //$(document).on('click', '#DeviceAdd', function () {
                //    DeviceAdd();
                //});
            }
        });


    }

    //弹窗工序列表展示
    window.ShowProcessProcedures = function ShowProcessProcedures() {
        layui.use(function () {
            var table = layui.table;
            var ProductionProcessesNumber = $("#ProductionProcessesNumber").val();

            //执行渲染
            table.render({
                elem: '#ProcessProceduresTable',
                page: {
                    limit: 15, //每页显示的条数
                    limits: [15, 20, 25, 30] //每页显示条数的选择项
                },
                cols: [[
                    { type: 'checkbox', fixed: 'left' },
                    { field: 'productionProcessesName', title: '工序名称', width: 130 },
                    { field: 'productionProcessesNumber', title: '工序编号', width: 130 },

                ]],
                //数据接口
                data: []
            });
            //请求数据
            $.ajax({
                url: '../Process/GetProductiList',
                type: "get",
                data: {
                    PageIndex: 1,
                    PageSize: 15,
                    ProductionProcessesName: "",
                    ProductionProcessesNumber: ProductionProcessesNumber,
                    Status: null
                },
                success: function (data) {
                    data = JSON.parse(data);
                    console.log(data);
                    table.reload('ProcessProceduresTable', {
                        pagge: {
                            limit: 15,
                            limits: [15, 20, 25, 30]
                        },
                        data: data.data.list
                    });                                                                           
                },
                error: function () {
                    console.log("请求失败");
                }

            });
        });
    }

    //    //确认选中数据
    //    $("#DeviceAdd").on('click', function () {
    //        DeviceAdd()            
    //})
    window.DeviceAdd = function DeviceAdd() {
        // 获取选中行的数据（这里主要是获取warehouseId）
        var checkStatus = layui.table.checkStatus('ProcessProceduresTable');
        var token = localStorage.getItem("token");

        var ids = checkStatus.data.map(function (item) {
            return item.productionProcessesId;
        });
        localStorage.setItem("ids",ids)
        var idd = ids
        console.log(idd);
        var ss = 0;
        var idsd = idd.map(function (item) {
           ss=item
           return item;
       
        });
        console.log(ss);
        if (ids.length == 0) {
            layer.msg("请至少选择一条要新增工序的数据");
            return;
        }
        
        $.ajax({
            type: "get",
            url: "../Process/ReverseGetByidProcess",
            data: {
                ProductionProcessesId: ss,
                token: localStorage.getItem("token")
            },
            success: function (res) {
                var ter = JSON.parse(res)
                console.log(ter.data)
                localStorage.setItem("data", ter.data)
                $(ter.data).each(function () {
                  
                    $('#process1').val(this.productionProcessesName);
                  
                    localStorage.setItem("productionProcessesName", this.productionProcessesName)
                    $('#ProductionProcessesNum').val(this.productionProcessesNumber);
                    $('#ProductionNotes').val(this.productionProcessesName);
                    $('#SpecNotes').val(this.specification);
                    $('#ProductioType').val(this.processClassification);
                  
                    localStorage.setItem("productionProcessesId", this.productionProcessesId)
                    console.log(localStorage.getItem("productionProcessesId"))
                })

            },
        }); 
    }
    //显示质检组
    $.ajax({
        url: "../Quality/DeviceDo",
        type: "get",
        data: { token: localStorage.getItem("token") },
        success: function (data) {
            var obj = JSON.parse(data);
            var options = '<option value="">请选择</option>'; // 添加默认选项
            $.each(obj.data.list, function (index, item) {
                console.log(item.qualitygroupId)
                localStorage.setItem("id",item.qualitygroupId)
                options += '<option value="' + item.qualitygroupId + '">' + item.qualitygroupName + '</option>';
            });
            $("#Qualitygroups").html(options);
           
            form.render('select'); // 重新渲染下拉框
           
        },
        error: function () {
            console.log("获取数据失败");
        }
    });


    //显示质检项目
    window.xianshi = function xianshi() {
layui.use(function () {
            var QualitygroupId = $("#Qualitygroups").val()
    console.log(QualitygroupId)
    localStorage.setItem("QualitygroupIds", QualitygroupId)
            // 先进行表格的初始渲染
            table.render({
                elem: '#allocationProcess1',  // 表格元素的选择器
               
                cols: [[
                    { type: 'checkbox', fixed: 'left' },
                    { field: 'qualityinspectionName', title: '质检项目名称' },
                    { field: 'qualityinspectionBH', title: '质检项目编号' },
                    { field: 'qualityinspectionmethood', title: '质检方法' },
                    { field: 'qualityinspectiontool', title: '质检工具' },
                    { field: 'qualityinspectionmetype', title: '质检类型' },
                    { field: 'standardvalue', title: '标准值' },
                    //{ field: 0, title: '上差值' },
                    //{ field: 0, title: '下差值' },
                    {
                        field: '', width: 260, title: '操作', templet: function (d) {
                            return '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="FanUpdate(' + d.qualityinspectionId + ')"><i class="layui-icon layui-icon-edit"></i>编辑</a> ' +
                                '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="IsDel(' + d.qualityinspectionId + ')"><i class="layui-icon layui-icon-delete"></i>移除</a>';
                        }

                    }
                ]],
                // 初始时可以先不加载数据，等待 ajax 请求成功后再更新
                data: []
            });
            // 创建表格实例
            $.ajax({
                url: "../Process/GetProList",
                type: "get",
                data: {
                    token: "",
                    QualitygroupId: QualitygroupId,
                },
                success: function (datas) {
                    datas = JSON.parse(datas);
                    console.log(datas);
                    // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
                    table.reload('allocationProcess1', {
                        //page: {
                        //    limit: 10,
                        //    limits: [5, 8, 10]
                        //},
                        data: datas
                    });
                },
                error: function () {
                    console.log("请求失败");
                }
            });
        });
    }
        
    
    //添加工艺信息
    form.on('submit(formSubmit)', function (data) {
        var field = data.field;
        var checkStatus = table.checkStatus('allocationProcess');
        field.allocationProdutesList = checkStatus.data.map(function (item) {
            return item;
        });
        console.log(field);//field.products
      
        obj1.processPouteNumber = $("#ProcessNo").val()
        obj1.processRouteName = $("#ProcessRouteName1").val()
        obj1.state = $("[name='State']:checked").val()
        obj1.productName = $("#ProductName1").val()
        obj1.createdBy = $("#CreatedB1").val()
        obj1.createdDate = $("#CreatedDate1").val()
        obj1.productionProcessesId = localStorage.getItem("ids")
        obj1.QualityprogramId = localStorage.getItem("QualitygroupIds")
        console.log(obj1)
        // 显示填写结果，仅作演示用
        $.ajax({
            url: '../Process/GetProcessAdd',
            type: 'post',
            data: {
                field: obj1, token: localStorage.getItem("token")
            },
            success: function (res) {
                var res = JSON.parse(res)
                console.log(field);
                console.log(res);
                if (res.code > 0) {
                    layer.alert("添加成功");
                    //location.href = '/Process/ShowProcess'
                } else {
                    layer.alert("添加失败");
                }
            }
        });

        return false; // 阻止默认 form 跳转
    });

})
